<?php

namespace app\web\controller;

use app\common\controller\Frontend;
use app\common\model\goods\GoodsHouseStock;
use app\common\model\stock\Stock as stockmodel;
use think\Db;

class Stock extends Frontend
{

    protected $noNeedLogin = 'login';
    protected $noNeedRight = '*';
    protected $layout = '';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }
    public function index(){
        $keywords = input('keywords');
        if(!empty($keywords)){
            $where['title|specs'] = array('like','%'.$keywords.'%');
        }
        $list = Db::name('stock_report')
            ->alias('r')
            ->field('r.*,s.name')
            ->where([])
            ->join('storehouse s','r.storehouse_id = s.id')
            ->order('r.id','desc')
            ->limit(0,10)
            ->select();
        $srows = Db::name('storehouse')->select();
        $total = Db::name('stock_report')->count();
        $this->assign('keywords',$keywords);
        $this->assign('srows',$srows);
        $this->assign('list',$list);
        $this->assign('total',$total);
        return $this->view->fetch();
    }
    public function addreport(){
        if ($this->request->isAjax()) {
            $storehouse_id = input('storehouse_id/d',0);
            $srow = Db::name('storehouse')->where(array('id'=>$storehouse_id))->find();
            if(empty($srow)){
                $this->error('仓库选择错误');
            }
            $group = Db::name('auth_group_access')->alias('a')->where(array('uid' => session('admin')['id']))->join('zax_auth_group g', 'a.group_id = g.id')->find();
            $num = Db::name('stock_report')->where(array('createtime'=>array('>',strtotime(date('Y-m-d',time())))))->count();
            $data = array(
                'number'=>'PK-'.date('Y-m-d').'-'.getnumber($num),
                'storehouse_id'=>$storehouse_id,
                'createtime'=>time(),
                'admin_id'=>session('admin')['id'],
                'group_id'=>$group['group_id'],
                'company'=>$group['name'],
                'operator'=>session('admin')['nickname'],
                'explain'=>input('explain'),
            );
            $res = Db::name('stock_report')->insert($data);
            if($res){
                $this->success('添加报表成功');
            }else{
                $this->error('添加报表失败');
            }
        }
    }
    public function reportlist(){

        $where = [];
        $keywords = input('keywords');
        if(!empty($keywords)){
            $where['title|specs'] = array('like','%'.$keywords.'%');
        }
        $report_id = input('report_id/d',0);

        $rrow = Db::name('stock_report')->where(array('id'=>$report_id))->find();

        $is_stock = input('is_stock/d',0);
        if($is_stock){
            $rgids = Db::name('stock_report_goods')->where(['report_id'=>$report_id])->column('goods_id');
            $where['goods_id'] =array('notin',$rgids);
        }
        if(!$rrow){
            $this->error('报表不存在');
        }
//        $where['s.stock'] = array('>',0);


        $list = Db::name('goods')
            ->alias('g')
            ->where($where)
            ->join('goods_house_stock s','g.id = s.goods_id')
            ->limit(0,10)
            ->select();
        $total = Db::name('goods')
            ->alias('g')
            ->where($where)
            ->join('goods_house_stock s','g.id = s.goods_id')
            ->count();
        $hrow = Db::name('storehouse')->where(array('id'=>$rrow['storehouse_id']))->find();

        $numbers = [];
        $ids = [];
        foreach ($list as &$v){
            $ids[] = $v['goods_id'];
            $numbers[] = $v['number'];
        }
        $prices = $this->getprice(implode(',',$numbers),$hrow['code']);
        $goodslist = Db::name('stock_report_goods')->where(['report_id'=>$report_id,'goods_id'=>array('in',$ids)])->select();
        $domain = $_SERVER['HTTP_HOST'];
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        foreach ($list as &$v){
            $v['now_stock'] = 0;
            $v['image'] = $this->thisurl.'/assets/web/images/zanwu.png';

            foreach ($prices as $val){
                if($val['spec_num'] == $v['number']){
                    $v['image'] = $protocol.$domain.$val['original_img'];
                }
            }
            foreach ($goodslist as $value){
                if($v['goods_id'] == $value['goods_id']){
                    $v['now_stock'] = $value['stock'];
                }
            }
        }

        $this->assign('total',$total);
        $this->assign('list',$list);
        $this->assign('keywords',$keywords);
        $this->assign('report_id',$report_id);
        $this->assign('is_stock',$is_stock);
        return $this->view->fetch();
    }
    public function getgoods(){
        $page = input('page/d',1);
        $pagesize = input('pagesize/d',10);
        $where = [];
        $keywords = input('keywords');
        if(!empty($keywords)){
            $where['title|specs'] = array('like','%'.$keywords.'%');
        }
        $report_id = input('report_id/d',0);
        $rrow = Db::name('stock_report')->where(array('id'=>$report_id))->find();
        $is_stock = input('is_stock/d',0);
        if($is_stock){
            $rgids = Db::name('stock_report_goods')->where(['report_id'=>$report_id])->column('goods_id');
            $where['goods_id'] =array('notin',$rgids);
        }
        if(!$rrow){
            $this->error('报表不存在');
        }

        $list = Db::name('goods')
            ->alias('g')
            ->where($where)
            ->join('goods_house_stock s','g.id = s.goods_id')
            ->limit($page*$pagesize,$pagesize)
            ->select();
        $hrow = Db::name('storehouse')->where(array('id'=>$rrow['storehouse_id']))->find();

        $numbers = [];
        $ids = [];
        foreach ($list as &$v){
            $ids[] = $v['goods_id'];
            $numbers[] = $v['number'];
        }
        $prices = $this->getprice(implode(',',$numbers),$hrow['code']);
        $goodslist = Db::name('stock_report_goods')->where(['report_id'=>$report_id,'goods_id'=>array('in',$ids)])->select();

        $domain = $_SERVER['HTTP_HOST'];
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        foreach ($list as &$v){
            $v['now_stock'] = 0;
            $v['image'] = $this->thisurl.'/assets/web/images/zanwu.png';

            foreach ($prices as $val){
                if($val['spec_num'] == $v['number']){
                    $v['image'] = $protocol.$domain.$val['original_img'];
                }
            }
            foreach ($goodslist as $value){
                if($v['goods_id'] == $value['goods_id']){
                    $v['now_stock'] = $value['stock'];
                }
            }
        }

        $this->success('获取成功','',$list);
    }
    public function addstock(){
        $model = new stockmodel();
        return $model->addstocks();
    }
}
